<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>constexpr 说明符(C++11 起)</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/constexpr","wgTitle":"cpp/language/constexpr","wgCurRevisionId":72571,"wgArticleId":2693,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Pages with unreviewed CWG DR marker"],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/constexpr","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><style type="text/css">/*<![CDATA[*/
.source-text {line-height: normal;}
.source-text li, .source-text pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for text
 * CSS class: source-text, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.text.source-text .de1, .text.source-text .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.text.source-text  {font-family:monospace;}
.text.source-text .imp {font-weight: bold; color: red;}
.text.source-text li, .text.source-text .li1 {font-weight: normal; vertical-align:top;}
.text.source-text .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.text.source-text .li2 {font-weight: bold; vertical-align:top;}
.text.source-text .ln-xtra, .text.source-text li.ln-xtra, .text.source-text div.ln-xtra {background-color: #ffc;}
.text.source-text span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_constexpr skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">constexpr 说明符<span class="t-mark-rev t-since-cxx11">(C++11 起)</span></h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../container.html" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="declarations.html" title="cpp/language/declarations">声明</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-h2"><td colspan="5"> 概览 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations">声明语法</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html#.E8.AF.B4.E6.98.8E.E7.AC.A6" title="cpp/language/declarations"> 声明说明符序列</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html#.E5.A3.B0.E6.98.8E.E7.AC.A6" title="cpp/language/declarations"> 声明符</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 说明符 </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="typedef.html" title="cpp/language/typedef"><span class="t-lines"><span>typedef</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="inline.html" title="cpp/language/inline"><span class="t-lines"><span>inline</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="virtual.html" title="cpp/language/virtual"> virtual 函数说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="explicit.html" title="cpp/language/explicit"> explicit 函数说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="friend.html" title="cpp/language/friend"><span class="t-lines"><span>friend</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><strong class="selflink"><span class="t-lines"><span>constexpr</span></span></strong></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="consteval.html" title="cpp/language/consteval"><span class="t-lines"><span>consteval</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="constinit.html" title="cpp/language/constinit"><span class="t-lines"><span>constinit</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="storage_duration.html" title="cpp/language/storage duration"> 存储类说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="tu_local.html" title="cpp/language/tu local">翻译单元局部</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="class.html" title="cpp/language/class"><span class="t-lines"><span>class</span><span>struct</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="union.html" title="cpp/language/union"><span class="t-lines"><span>union</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="enum.html" title="cpp/language/enum"><span class="t-lines"><span>enum</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="decltype.html" title="cpp/language/decltype"><span class="t-lines"><span>decltype</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="auto.html" title="cpp/language/auto"><span class="t-lines"><span>auto</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="alignas.html" title="cpp/language/alignas"><span class="t-lines"><span>alignas</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="cv.html" title="cpp/language/cv"><span class="t-lines"><span>const</span><span>volatile</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="elaborated_type_specifier.html" title="cpp/language/elaborated type specifier">详述类型说明符</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="attributes.html" title="cpp/language/attributes"> 属性</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-h2"><td colspan="5"> 声明符 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="reference.html" title="cpp/language/reference"> 引用</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="pointer.html" title="cpp/language/pointer"> 指针</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="array.html" title="cpp/language/array"> 数组</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 块声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 简单声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="structured_binding.html" title="cpp/language/structured binding">结构化绑定声明</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="type_alias.html" title="cpp/language/type alias"> 别名声明</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace_alias.html" title="cpp/language/namespace alias"> 命名空间别名定义</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="using_declaration.html" title="cpp/language/using declaration"> using 声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace.html#using_.E6.8C.87.E4.BB.A4" title="cpp/language/namespace"> using 指令</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static_assert.html" title="cpp/language/static assert"> static_assert 声明</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="asm.html" title="cpp/language/asm"> asm 声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="enum.html" title="cpp/language/enum"> 不可见 enum 声明</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 其他声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace.html" title="cpp/language/namespace"> 命名空间定义</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html" title="cpp/language/function"> 函数声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html" title="cpp/language/class template"> 类模板声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function_template.html" title="cpp/language/function template"> 函数模板声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html#.E6.98.BE.E5.BC.8F.E5.AE.9E.E4.BE.8B.E5.8C.96" title="cpp/language/class template"> 显式模板实例化</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="template_specialization.html" title="cpp/language/template specialization"> 显式模板特化</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="language_linkage.html" title="cpp/language/language linkage"> 链接说明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 属性声明</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 空声明</a> </td></tr>
</table></div></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<dl><dd><ul><li><code><b>constexpr</b></code> - 指定变量或函数的值可以在<a href="constant_expression.html" title="cpp/language/constant expression">常量表达式</a>中出现
</li></ul>
</dd></dl>
<h3><span class="mw-headline" id=".E8.A7.A3.E9.87.8A">解释</span></h3>
<p><code>constexpr</code> 说明符声明编译时可以对函数或变量求值值。这些变量和函数（给定了合适的函数实参的情况下）即可用于需要编译期<a href="constant_expression.html" title="cpp/language/constant expression">常量表达式</a>的地方。
</p><p>声明对象<span class="t-rev-inl t-until-cxx14"><span>或非静态成员函数</span> <span><span class="t-mark-rev t-until-cxx14">(C++14 前)</span></span></span>时使用 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">constexpr</span></span></span> 说明符则同时蕴含 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">const</span></span></span>。声明函数<span class="t-rev-inl t-since-cxx17"><span>或<a href="static.html" title="cpp/language/static">静态</a>成员变量</span> <span><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></span></span>时使用 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">constexpr</span></span></span> 说明符则同时蕴含 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">inline</span></span></span>。如果一个函数或函数模板的某个声明拥有 <code>constexpr</code> 说明符，则其所有声明都必须含有该说明符。
</p><p><b><span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">constexpr</span></span></span> 变量</b>必须满足下列要求：
</p>
<dl><dd><ul><li> 它的类型必须是<a href="../named_req/LiteralType.html" title="cpp/named req/LiteralType"><span style="font-style:italic">字面类型</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(LiteralType)</span> </a>。
</li><li> 它必须被立即初始化。
</li><li> 它的初始化包括所有隐式转换、构造函数调用等的<a href="eval_order.html" title="cpp/language/eval order">全表达式</a>必须是<a href="constant_expression.html" title="cpp/language/constant expression">常量表达式</a>
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<p><span id=".E5.B8.B8.E9.87.8F.E6.9E.90.E6.9E.84"></span>
</p>
<dl><dd><ul><li> 它必须拥有常量析构，即：
</li></ul>
<dl><dd><ul><li> 它不是类类型亦或其（可能多维的）数组，或
</li><li> 它是类类型或其（可能多维的）数组，且该类类型拥有 constexpr 析构函数；对于作用仅为销毁该对象的虚设表达式 <span class="t-c"><span class="mw-geshi cpp source-cpp">e</span></span> ，如果该对象与其非 mutable 子对象（但非其 mutable 子对象）的生存期始于 <span class="t-c"><span class="mw-geshi cpp source-cpp">e</span></span> 内，那么 <span class="t-c"><span class="mw-geshi cpp source-cpp">e</span></span> 是核心常量表达式。
</li></ul>
</dd></dl>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<p>如果 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">constexpr</span></span></span> 变量非<a href="tu_local.html" title="cpp/language/tu local">翻译单元局部</a>，那么它不应被初始化为指向或指代可用于常量表达式的翻译单元局部实体，或拥有指向或指代这种实体的（可能递归的）子对象。这种初始化在<a href="modules.html" title="cpp/language/modules">模块接口单元</a>（在其私有模块片段外，如果存在）或模块划分中被禁止，并在任何其他语境中被弃用。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
<p><b><span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">constexpr</span></span></span> 函数</b>必须满足下列要求：
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx20"><td>
<dl><dd><ul><li> 它必须非<a href="virtual.html" title="cpp/language/virtual">虚</a>
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-until-cxx20">(C++20 前)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<dl><dd><ul><li> 它必须不是<a href="coroutines.html" title="cpp/language/coroutines">协程</a>
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
<dl><dd><ul><li> 它的返回类型（如果存在）必须是<a href="../named_req/LiteralType.html" title="cpp/named req/LiteralType"><span style="font-style:italic">字面类型</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(LiteralType)</span> </a>
</li><li> 它的每个参数都必须是<a href="../named_req/LiteralType.html" title="cpp/named req/LiteralType"><span style="font-style:italic">字面类型</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(LiteralType)</span> </a>
</li><li> 对于构造函数<span class="t-rev-inl t-since-cxx20"><span>与析构函数</span> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></span></span>，该类必须无虚基类
</li><li> 至少存在一组实参值，使得函数的一个调用为<a href="constant_expression.html#.E6.A0.B8.E5.BF.83.E5.B8.B8.E9.87.8F.E8.A1.A8.E8.BE.BE.E5.BC.8F" title="cpp/language/constant expression">核心常量表达式</a>的被求值的子表达式<span class="t-rev-inl t-since-cxx14"><span>（对于构造函数为足以用于<a href="constant_initialization.html" title="cpp/language/constant initialization">常量初始化器</a>）</span> <span><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></span></span>。不要求诊断是否违反这点。
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx20"><td>
<dl><dd><ul><li> 它的函数体必须不是<a href="function-try-block.html" title="cpp/language/function-try-block">函数 try 块</a>
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-until-cxx20">(C++20 前)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx14"><td>
<dl><dd><ul><li> 函数体必须被弃置或预置，或只含有下列内容：
</li></ul>
<dl><dd><ul><li> <a href="statements.html#.E8.A1.A8.E8.BE.BE.E5.BC.8F.E8.AF.AD.E5.8F.A5" title="cpp/language/statements">空语句</a>（仅分号）
</li><li> <a href="static_assert.html" title="cpp/language/static assert">static_assert</a> 声明
</li><li> 不定义类或枚举的 <a href="typedef.html" title="cpp/language/typedef">typedef</a> 声明及<a href="type_alias.html" title="cpp/language/type alias">别名</a>声明
</li><li> <a href="namespace.html#using_.E5.A3.B0.E6.98.8E" title="cpp/language/namespace">using 声明</a>
</li><li> <a href="namespace.html#using_.E6.8C.87.E4.BB.A4" title="cpp/language/namespace">using 指令</a>
</li><li> 恰好一条 <a href="return.html" title="cpp/language/return">return</a> 语句，当函数不是构造函数时。
</li></ul>
</dd></dl>
</dd></dl>
</td>
<td><span class="t-mark-rev t-until-cxx14">(C++14 前)</span></td></tr>
<tr class="t-rev t-since-cxx14 t-until-cxx23"><td>
<p><span id="relaxed-constexpr"></span>
</p>
<dl><dd><ul><li> 函数体必须<b>不</b>含：
</li></ul>
<dl><dd><ul><li> <a href="goto.html" title="cpp/language/goto">goto</a> 语句
</li><li> 拥有除 <code>case</code> 和 <code>default</code> 之外的带有<a href="statements.html#.E6.A0.87.E7.AD.BE" title="cpp/language/statements">标签</a>的语句
</li></ul>
</dd></dl>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx20"><td>
<dl><dd><dl><dd><ul><li> <a href="try_catch.html" title="cpp/language/try catch">try 块</a>
</li><li> <a href="asm.html" title="cpp/language/asm">asm 声明</a>
</li><li> <a href="default_initialization.html" title="cpp/language/default initialization">不进行初始化</a>的变量定义
</li></ul>
</dd></dl>
</dd></dl>
</td>
<td><span class="t-mark-rev t-until-cxx20">(C++20 前)</span></td></tr>
</table>
<dl><dd><dl><dd><ul><li> 非字面类型的变量定义
</li><li> 静态或线程<a href="storage_duration.html" title="cpp/language/storage duration">存储期</a>变量的定义 
</li></ul>
</dd><dd> （是 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy1">=</span><span class="kw1">default</span><span class="sy4">;</span></span></span> 或 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy1">=</span>delete<span class="sy4">;</span></span></span> 的函数体均不含任何上述内容。）
</dd></dl>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx14">(C++14 起)</span><br><span class="t-mark-rev t-until-cxx23">(C++23 前)</span></td></tr>
</table>
<p>函数体非 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy1">=</span>delete<span class="sy4">;</span></span></span> 的 <b><span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">constexpr</span></span></span> 构造函数</b>必须满足下列额外要求：
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx20"><td>
<dl><dd><ul><li> 对于<a href="class.html" title="cpp/language/class">类或结构体</a>的构造函数，每个子对象和每个<a href="union.html#.E8.81.94.E5.90.88.E4.BD.93.E5.BC.8F.E7.9A.84.E7.B1.BB" title="cpp/language/union">非变体</a>非静态数据成员必须被初始化。若类是<a href="union.html#.E8.81.94.E5.90.88.E4.BD.93.E5.BC.8F.E7.9A.84.E7.B1.BB" title="cpp/language/union">联合体式的类</a>，对于其每个非空匿名联合体成员，必须恰好有一个变体成员被初始化
</li><li> 对于非空<a href="union.html" title="cpp/language/union">联合体</a>的构造函数，恰好有一个非静态数据成员被初始化
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-until-cxx20">(C++20 前)</span></td></tr>
</table>
<dl><dd><ul><li> 每个被选用于初始化非静态成员和基类的构造函数必须是 constexpr 构造函数。
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx20"><td>
<p>析构函数不能为 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">constexpr</span></span></span>，但能在常量表达式中隐式调用<a href="destructor.html#.E5.B9.B3.E5.87.A1.E6.9E.90.E6.9E.84.E5.87.BD.E6.95.B0" title="cpp/language/destructor">平凡析构函数</a>。
</p>
</td>
<td><span class="t-mark-rev t-until-cxx20">(C++20 前)</span></td></tr>
<tr class="t-rev t-since-cxx20"><td>
<p>函数体非 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy1">=</span>delete<span class="sy4">;</span></span></span> 的 <b><span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">constexpr</span></span></span> 析构函数</b>必须满足下列额外要求：
</p>
<dl><dd><ul><li> 每个用于销毁非静态数据成员与基类的析构函数必须是 constexpr 析构函数。
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
<p>对于 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">constexpr</span></span></span> 函数模板和类模板的 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">constexpr</span></span></span> 函数成员，必须至少有一个特化满足上述要求。其他特化仍被认为是 constexpr 的，尽管常量表达式中不能出现这种函数的调用。
</p>
<h3><span class="mw-headline" id=".E6.B3.A8.E8.A7.A3">注解</span></h3>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx17"><td>
<p>因为 <a href="noexcept.html" title="cpp/language/noexcept">noexcept</a> 运算符始终对常量表达式返回 <code>true</code>，所以它可以用于检查具体特定的 constexpr 函数调用是否采用常量表达式分支：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">constexpr</span> <span class="kw4">int</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> 
<span class="kw4">constexpr</span> <span class="kw4">bool</span> b1 <span class="sy1">=</span> <span class="kw1">noexcept</span><span class="br0">(</span>f<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// false，constexpr 函数未定义</span>
<span class="kw4">constexpr</span> <span class="kw4">int</span> f<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">0</span><span class="sy4">;</span> <span class="br0">}</span>
<span class="kw4">constexpr</span> <span class="kw4">bool</span> b2 <span class="sy1">=</span> <span class="kw1">noexcept</span><span class="br0">(</span>f<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// true，f() 是常量表达式</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-until-cxx17">(C++17 前)</span></td></tr>
</table>
<p>constexpr 构造函数允许用于非字面类型的类。例如，<span class="t-lc"><a href="../memory/unique_ptr.html" title="cpp/memory/unique ptr">std::unique_ptr</a></span> 的默认构造函数是 constexpr，允许<a href="constant_initialization.html" title="cpp/language/constant initialization">常量初始化</a>。
</p><p>引用变量可声明为 constexpr（其初始化器必须是<a href="constant_expression.html#.E5.B8.B8.E9.87.8F.E8.A1.A8.E8.BE.BE.E5.BC.8F" title="cpp/language/constant expression">引用常量表达式</a>）：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">static</span> <span class="kw4">constexpr</span> <span class="kw4">int</span> <span class="kw4">const</span><span class="sy3">&amp;</span> x <span class="sy1">=</span> <span class="nu0">42</span><span class="sy4">;</span> <span class="co1">// 到 const int 对象的 constexpr 引用</span>
                                    <span class="co1">// （该对象拥有静态存储期，因为静态引用延长了生存期）</span></pre></div></div>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<p>尽管在 constexpr 函数中允许 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">try</span></span></span> 块与内联汇编，但是常量表达式中仍然不允许抛出异常或执行汇编。
</p><p>若变量拥有常量析构，则无需为调用其析构函数而生成机器码，即使其析构函数非平凡。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E5.85.B3.E9.94.AE.E8.AF.8D">关键词</span></h3>
<p><a href="../keyword/constexpr.html" title="cpp/keyword/constexpr"><tt>constexpr</tt></a>
</p>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</span></h3>
<div class="t-example"><p>计算阶乘的 C++11 constexpr 函数的定义，及扩展字符串字面量的字面类型：</p><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;stdexcept&gt;</span>
 
<span class="co1">// C++11 constexpr 函数使用递归而非迭代</span>
<span class="co1">// （C++14 constexpr 函数可使用局部变量和循环）</span>
<span class="kw4">constexpr</span> <span class="kw4">int</span> factorial<span class="br0">(</span><span class="kw4">int</span> n<span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw1">return</span> n <span class="sy1">&lt;=</span> <span class="nu0">1</span> <span class="sy4">?</span> <span class="nu0">1</span> <span class="sy4">:</span> <span class="br0">(</span>n <span class="sy2">*</span> factorial<span class="br0">(</span>n <span class="sy2">-</span> <span class="nu0">1</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="co1">// 字面类</span>
<span class="kw1">class</span> conststr <span class="br0">{</span>
    <span class="kw4">const</span> <span class="kw4">char</span><span class="sy2">*</span> p<span class="sy4">;</span>
    <a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> sz<span class="sy4">;</span>
<span class="kw1">public</span><span class="sy4">:</span>
    <span class="kw1">template</span><span class="sy1">&lt;</span><a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> N<span class="sy1">&gt;</span>
    <span class="kw4">constexpr</span> conststr<span class="br0">(</span><span class="kw4">const</span> <span class="kw4">char</span><span class="br0">(</span><span class="sy3">&amp;</span>a<span class="br0">)</span><span class="br0">[</span>N<span class="br0">]</span><span class="br0">)</span><span class="sy4">:</span> p<span class="br0">(</span>a<span class="br0">)</span>, sz<span class="br0">(</span>N <span class="sy2">-</span> <span class="nu0">1</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>
 
    <span class="co1">// constexpr 函数通过抛异常来提示错误</span>
    <span class="co1">// C++11 中，它们必须用条件运算符 ?: 来这么做</span>
    <span class="kw4">constexpr</span> <span class="kw4">char</span> operator<span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> n<span class="br0">)</span> <span class="kw4">const</span>
    <span class="br0">{</span>
        <span class="kw1">return</span> n <span class="sy1">&lt;</span> sz <span class="sy4">?</span> p<span class="br0">[</span>n<span class="br0">]</span> <span class="sy4">:</span> <span class="kw1">throw</span> <a href="../error/out_of_range.html"><span class="kw768">std::<span class="me2">out_of_range</span></span></a><span class="br0">(</span><span class="st0">""</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="br0">}</span>
    <span class="kw4">constexpr</span> <a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> size<span class="br0">(</span><span class="br0">)</span> <span class="kw4">const</span> <span class="br0">{</span> <span class="kw1">return</span> sz<span class="sy4">;</span> <span class="br0">}</span>
<span class="br0">}</span><span class="sy4">;</span>
 
<span class="co1">// C++11 constexpr 函数必须把一切放在单条 return 语句中</span>
<span class="co1">// （C++14 无此要求）</span>
<span class="kw4">constexpr</span> <a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> countlower<span class="br0">(</span>conststr s, <a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> n <span class="sy1">=</span> <span class="nu0">0</span>,
                                             <a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> c <span class="sy1">=</span> <span class="nu0">0</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw1">return</span> n <span class="sy1">==</span> s.<span class="me1">size</span><span class="br0">(</span><span class="br0">)</span> <span class="sy4">?</span> c <span class="sy4">:</span>
           <span class="st0">'a'</span> <span class="sy1">&lt;=</span> s<span class="br0">[</span>n<span class="br0">]</span> <span class="sy3">&amp;&amp;</span> s<span class="br0">[</span>n<span class="br0">]</span> <span class="sy1">&lt;=</span> <span class="st0">'z'</span> <span class="sy4">?</span> countlower<span class="br0">(</span>s, n <span class="sy2">+</span> <span class="nu0">1</span>, c <span class="sy2">+</span> <span class="nu0">1</span><span class="br0">)</span> <span class="sy4">:</span>
                                        countlower<span class="br0">(</span>s, n <span class="sy2">+</span> <span class="nu0">1</span>, c<span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="co1">// 输出要求编译时常量的函数，用于测试</span>
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw4">int</span> n<span class="sy1">&gt;</span>
<span class="kw1">struct</span> constN
<span class="br0">{</span>
    constN<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> n <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span> <span class="br0">}</span>
<span class="br0">}</span><span class="sy4">;</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"4! = "</span> <span class="sy4">;</span>
    constN<span class="sy1">&lt;</span>factorial<span class="br0">(</span><span class="nu0">4</span><span class="br0">)</span><span class="sy1">&gt;</span> out1<span class="sy4">;</span> <span class="co1">// 在编译时计算</span>
 
    <span class="kw4">volatile</span> <span class="kw4">int</span> k <span class="sy1">=</span> <span class="nu0">8</span><span class="sy4">;</span> <span class="co1">// 使用 volatile 防止优化</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> k <span class="sy1">&lt;&lt;</span> <span class="st0">"! = "</span> <span class="sy1">&lt;&lt;</span> factorial<span class="br0">(</span>k<span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span> <span class="co1">// 运行时计算</span>
 
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"<span class="es1">\"</span>Hello, world!<span class="es1">\"</span> 里小写字母的个数是 "</span><span class="sy4">;</span>
    constN<span class="sy1">&lt;</span>countlower<span class="br0">(</span><span class="st0">"Hello, world!"</span><span class="br0">)</span><span class="sy1">&gt;</span> out2<span class="sy4">;</span> <span class="co1">// 隐式转换到常量字符串</span>
<span class="br0">}</span></pre></div></div>
<p>输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">4! = 24
8! = 40320
"Hello, world!" 里小写字母的个数是 9</pre></div></div> 
</div>
<h3><span class="mw-headline" id=".E7.BC.BA.E9.99.B7.E6.8A.A5.E5.91.8A">缺陷报告</span></h3>
<p>下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th> DR
</th>
<th> 应用于
</th>
<th> 出版时的行为
</th>
<th> 正确行为
</th></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue1911">CWG 1911</a>
</td>
<td> C++11
</td>
<td> 非字面类型不允许拥有 constexpr 构造函数
</td>
<td> 在常量初始化中允许
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue2004">CWG 2004</a>
</td>
<td> C++14
</td>
<td> 在常量表达式中允许复制/移动有 mutable 成员的联合体
</td>
<td> mutable 变体现在无法被隐式复制/移动
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue2163">CWG 2163</a>
</td>
<td> C++14
</td>
<td> constexpr 函数中禁止 goto，但允许标号
</td>
<td> 标号也被禁止
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue2268">CWG 2268</a>
</td>
<td> C++14
</td>
<td> cwg 2004 曾禁止了复制/移动有 mutable 成员的联合体
</td>
<td> 在该对象在常量表达式中创建的情况下允许
</td></tr></table>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<ul><li> <a href="constant_expression.html" title="cpp/language/constant expression">常量表达式</a>
</li><li> <a href="consteval.html" title="cpp/language/consteval"><code>consteval</code></a>
</li><li> <a href="constinit.html" title="cpp/language/constinit"><code>constinit</code></a>
</li></ul>

<!-- 
NewPP limit report
Preprocessor visited node count: 4546/1000000
Preprocessor generated node count: 9467/1000000
Post‐expand include size: 93578/2097152 bytes
Template argument size: 27234/2097152 bytes
Highest expansion depth: 15/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:2693-0!*!0!!zh!*!zh!* and timestamp 20211125193129 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/constexpr&amp;oldid=72571">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/constexpr&amp;oldid=72571</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/constexpr">Online version</a></li><li>Offline version retrieved 2022-01-01 00:37.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2021年10月7日 (星期四) 21:37。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过57,652次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 1.570 secs. -->
	</body>
<!-- Cached 20211125193129 -->
</html>